#include "HallConfig.h"
#include "pugixml.hpp"
using namespace std;

SINGLETON_CLASS_INIT(CHallConfig);
    
bool CHallConfig::Load(u64 sid, const char *file)
{
    pugi::xml_document doc;
	pugi::xml_parse_result result = doc.load_file( file );
    if( result.status != pugi::status_ok )
    {
        printf( "CHallConfig::Load[%s] Error[%d]\n", file, (int)result );
        return false;
    }

    pugi::xml_node cfg = doc.child( "Config" );
	if( cfg.empty() )
	{
        printf( "CHallConfig::Load [%s] Error, not found child:Config\n", file );
		return false;
	}
	
    bool ret = false;
    for( pugi::xml_node item = cfg.first_child(); item; item = item.next_sibling() )
	{
		u64 serverid = (u64)item.attribute("sid").as_double();
        if( serverid != sid )
        {
            continue;
        }

        m_cfg.gameId = item.attribute("gameId").as_int();
        m_cfg.gameMode = item.attribute("gameMode").as_int();
        m_cfg.roomMode = item.attribute("roomMode").as_int();
        m_cfg.level = item.attribute("level").as_int();
        m_cfg.ante = item.attribute("ante").as_int();
        m_cfg.taxMultiple = item.attribute("taxMultiple").as_int();
        m_cfg.roomCount = item.attribute("roomCount").as_int();
        m_cfg.minPlayerCount = item.attribute("minPlayerCount").as_int();
        m_cfg.maxPlayerCount = item.attribute("maxPlayerCount").as_int();
        m_cfg.maxBeholderCount = item.attribute("maxBeholderCount").as_int();
        m_cfg.teamQueueCount = item.attribute("teamQueueCount").as_int();
        if( m_cfg.teamQueueCount < 0 || m_cfg.teamQueueCount >= E_MatchMode_Count )
        {
            m_cfg.teamQueueCount = E_MatchMode_FixedUser;
        }
        m_cfg.teamMatchMode = item.attribute("teamMatchMode").as_int();
        m_cfg.dbSid = (u64)item.attribute("dbSid").as_double();
        m_cfg.centerSid = (u64)item.attribute("centerSid").as_double();
        m_cfg.roomCenterSid = (u64)item.attribute("roomCenterSid").as_double();
        m_cfg.recordSid = (u64)item.attribute("recordSid").as_double();
        m_cfg.balanceSid = (u64)item.attribute("balanceSid").as_double();

        m_cfg.roomMoneyUpLimit = item.attribute("roomMoneyUpLimit").as_int();
        m_cfg.roomMoneyBottomLimit = item.attribute("roomMoneyBottomLimit").as_int();

        m_cfg.friendRoomExpireSec = item.attribute("friendRoomExpireSec").as_int();
        if( m_cfg.friendRoomExpireSec <= 0 )
        {
            m_cfg.friendRoomExpireSec = 86400; // 24h
        }

        m_cfg.friendRoomIdleExpireSec = item.attribute("friendRoomIdleExpireSec").as_int();
        if( m_cfg.friendRoomIdleExpireSec <= 0 )
        {
            m_cfg.friendRoomIdleExpireSec = 3600; // 1h
        }

        m_cfg.pauseDismissRoom = item.attribute("pauseDismissRoom").as_int();
        if( m_cfg.pauseDismissRoom <= 0 )
        {
            m_cfg.pauseDismissRoom = 0;
        }
        else
        {
            m_cfg.pauseDismissRoom = 1;
        }

        m_cfg.interactItemPrice = item.attribute("interactItemPrice").as_int();
        if( m_cfg.interactItemPrice < 0 )
        {
            m_cfg.interactItemPrice = 5;
        }

        m_cfg.useInteractItemInterval = item.attribute("useInteractItemInterval").as_int();
        if( m_cfg.useInteractItemInterval < 0 )
        {
            m_cfg.useInteractItemInterval = 2;
        }

        m_cfg.param1 = (u64)item.attribute("param1").as_double();
        m_cfg.param2 = (u64)item.attribute("param2").as_double();
        m_cfg.param3 = (u64)item.attribute("param3").as_double();
        m_cfg.param4 = (u64)item.attribute("param4").as_double();
        m_cfg.param5 = (u64)item.attribute("param5").as_double();

        m_cfg.udpIp = item.attribute("udpIp").as_string();
        if( m_cfg.udpIp == "" ) m_cfg.udpIp = "127.0.0.1";
        m_cfg.udpPort = item.attribute("udpPort").as_int();
        if( m_cfg.udpPort == 0 ) m_cfg.udpPort = 39001;
        m_cfg.udpPasswd = item.attribute("udpPasswd").as_string();
        if( m_cfg.udpPasswd == "" ) m_cfg.udpPasswd = "======AC>>>>>>";

        ret = true;
        break;
    }

    return ret;
}

